package com.example.myapplication;

import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.ui.PlayerView;

import java.util.List;

public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.VideoViewHolder> {
    private List<Video> videoList;
    private Context context;
    private ExoPlayer currentPlayer;  // 记录当前播放的播放器实例

    public VideoAdapter(List<Video> videoList, Context context) {
        this.videoList = videoList;
        this.context = context;
    }

    @NonNull
    @Override
    public VideoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_video, parent, false);
        return new VideoViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull VideoViewHolder holder, int position) {
        holder.bind(videoList.get(position));
    }

    @Override
    public int getItemCount() {
        return videoList.size();
    }

    class VideoViewHolder extends RecyclerView.ViewHolder {
        PlayerView playerView;
        TextView userNameTextView;
        TextView likeCountTextView;
        TextView commentCountTextView;
        ImageView userAvatarImageView;
        ImageView likeIcon;
        SimpleExoPlayer exoPlayer;

        public VideoViewHolder(@NonNull View itemView) {
            super(itemView);
            playerView = itemView.findViewById(R.id.playerView);
            userNameTextView = itemView.findViewById(R.id.userNameTextView);
            likeCountTextView = itemView.findViewById(R.id.likeCountTextView);
            commentCountTextView = itemView.findViewById(R.id.commentCountTextView);
            userAvatarImageView = itemView.findViewById(R.id.userAvatarImageView);
            likeIcon=itemView.findViewById(R.id.likeIcon);

            // 设置点击监听器
            likeIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 获取当前的视频对象
                    Video video = videoList.get(getAdapterPosition());

                    // 切换喜欢状态
                    boolean isLiked = video.isLiked();
                    video.setLiked(!isLiked); // 切换喜欢状态

                    // 根据新的喜欢状态更新喜欢数值
                    if (!isLiked) {
                        // 如果之前是已点赞，则减少点赞数
                        video.setLikeCount(video.getLikeCount() - 1);
                    } else {
                        // 如果之前是未点赞，则增加点赞数
                        video.setLikeCount(video.getLikeCount() + 1);
                    }

                    // 更新显示的喜欢数
                    likeCountTextView.setText(String.valueOf(video.getLikeCount()));

                    // 更新图标
                    updateLikeIcon(video);
                }
            });
        }

        private void updateLikeIcon(Video video) {
            if (video.isLiked()) {
                likeIcon.setImageResource(R.drawable.like); // 更改为已喜欢图标


            } else {
                likeIcon.setImageResource(R.drawable.likeclick); // 更改为未喜欢图标
            }
        }
        public void bind(Video video) {
            // 暂停当前播放的视频
            if (currentPlayer != null && currentPlayer != exoPlayer) {
                currentPlayer.pause();
            }

            // 使用 ExoPlayer 播放新的视频
            Uri videoUri = Uri.parse("http://192.168.110.148:8080" + video.getUrl());
            exoPlayer = new SimpleExoPlayer.Builder(context).build();

            // 设置媒体项
            MediaItem mediaItem = MediaItem.fromUri(videoUri);
            exoPlayer.setMediaItem(mediaItem);

            // 设置 PlayerView 显示
            playerView.setPlayer(exoPlayer);
            exoPlayer.prepare();
            exoPlayer.play();

            // 更新当前播放的视频播放器实例
            currentPlayer = exoPlayer;

            // 设置用户信息
            userNameTextView.setText(video.getUserName());
            likeCountTextView.setText(String.valueOf(video.getLikeCount()));
            commentCountTextView.setText(String.valueOf(video.getCommentCount()));

            Uri avatarUrl = Uri.parse("http://192.168.110.148:8080" + video.getAvter());
//            String avatarUrl = "http://192.168.110.148:8080" + video.getHead();
            Log.d("AvatarURL", avatarUrl.toString()); // 输出头像地址到日志
            Glide.with(context)
                    .load(avatarUrl)
                    .circleCrop()
                    .placeholder(R.drawable.ic_launcher_background) // 加载中的占位图
                    .error(R.drawable.ic_launcher_background) // 加载失败时的占位图
                    .into(userAvatarImageView);
            //Toast.makeText(context, avatarUrl.toString(), Toast.LENGTH_LONG).show();

            // 根据喜欢状态更新图标
            updateLikeIcon(video);

        }

        // 在 ViewHolder 销毁时释放 ExoPlayer
        public void releasePlayer() {
            if (exoPlayer != null) {
                exoPlayer.release();
            }
        }
    }

    @Override
    public void onViewRecycled(@NonNull VideoViewHolder holder) {
        super.onViewRecycled(holder);
        holder.releasePlayer();  // 回收时释放 ExoPlayer
    }
}

